package com.ljtxxhwj.myblob.controller;

import com.ljtxxhwj.myblob.entity.FavoriteFolder;
import com.ljtxxhwj.myblob.service.FavoriteFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/favorite-folders")
public class FavoriteFolderController {

    private final FavoriteFolderService favoriteFolderService;

    @Autowired
    public FavoriteFolderController(FavoriteFolderService favoriteFolderService) {
        this.favoriteFolderService = favoriteFolderService;
    }

    // 根据收藏夹名称查询收藏夹
    @GetMapping("/name/{favoriteFolderName}.do")
    public FavoriteFolder getFavoriteFolderByName(@PathVariable String favoriteFolderName) {
        return favoriteFolderService.getFavoriteFolderByName(favoriteFolderName);
    }

    // 插入一个新的收藏夹
    @PostMapping
    public ResponseEntity<FavoriteFolder> addFavoriteFolder(@RequestBody FavoriteFolder favoriteFolder) {
        Timestamp folderTime= new Timestamp(new java.util.Date().getTime());

        favoriteFolder.setFavoriteFolderDate(folderTime);
        favoriteFolderService.addFavoriteFolder(favoriteFolder);
        return new ResponseEntity<>(favoriteFolder, HttpStatus.CREATED);
    }

    // 更新收藏夹名称
    @PutMapping("/{favoriteFolderId}.do")
    public ResponseEntity<String> updateFavoriteFolderName(
            @PathVariable Integer favoriteFolderId,
            @RequestBody FavoriteFolder updatedFavoriteFolder) {
        favoriteFolderService.updateFavoriteFolderName(favoriteFolderId, updatedFavoriteFolder.getFavoriteFolderName());
        return new ResponseEntity<>("Favorite folder name updated successfully", HttpStatus.OK);
    }

    // 根据收藏夹ID删除收藏夹
    @DeleteMapping("/{favoriteFolderId}.do")
    public ResponseEntity<String> deleteFavoriteFolderById(@PathVariable Integer favoriteFolderId) {
        favoriteFolderService.deleteFavoriteFolderById(favoriteFolderId);
        return new ResponseEntity<>("Favorite folder deleted successfully", HttpStatus.OK);
    }

    // 查询所有收藏夹
    @GetMapping("getAllFavoriteFolders.do")
    public List<FavoriteFolder> getAllFavoriteFolders() {
        System.out.println(favoriteFolderService.getAllFavoriteFolders());
        return favoriteFolderService.getAllFavoriteFolders();
    }
}